package com.example.finalwork.Income;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.finalwork.DAO.IncomeDAO;
import com.example.finalwork.Entity.Income;
import com.example.finalwork.LoginActivity;
import com.example.finalwork.R;
import com.example.finalwork.lmpl.IncomeDAOlmpl;

import java.util.List;

import static org.junit.Assert.assertNotEquals;

public class IncomeAddActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = "IncomeAddActivity";
    private Button income_add_button,income_exit;
    private EditText income_NameText,income_DateText,income_AmountText,income_RemarkText;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_incomeadd);
        findViewById(R.id.pay_add_button).setOnClickListener(this);
        findViewById(R.id.pay_exit1).setOnClickListener(this);

        //Intent intent = getIntent();
        //String data = intent.getStringExtra("UserName");
        //Toast.makeText(this,data,Toast.LENGTH_SHORT).show();

        income_NameText = findViewById(R.id.pay_nametext);
        income_DateText = findViewById(R.id.pay_datetext);
        income_AmountText = findViewById(R.id.pay_amounttext);
        income_RemarkText = findViewById(R.id.pay_remarktext);

        setTitle("增加收入项目功能");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.pay_add_button:
                if(income_NameText.getText().toString().equals(""))
                {
                    Toast.makeText(this,"收入详细不能为空",Toast.LENGTH_SHORT).show();
                    income_NameText.requestFocus();
                    break;
                }
                else if(income_DateText.getText().toString().equals(""))
                {
                    Toast.makeText(this,"收入日期不能为空",Toast.LENGTH_SHORT).show();
                    income_DateText.requestFocus();
                    break;
                }
                else if(income_AmountText.getText().toString().equals(""))
                {
                    Toast.makeText(this,"收入金额不能为空",Toast.LENGTH_SHORT).show();
                    income_AmountText.requestFocus();
                    break;
                }
                if(income_RemarkText.getText().toString().equals(""))
                {
                    income_RemarkText.setText("无");
                }
                IncomeDAO incomeDAO = new IncomeDAOlmpl(this);
                List<Income> incomes = incomeDAO.GetAllIncome();
                Income income = new Income();
                income.setIn_id(incomes.size() + 1);
                income.setIn_User(LoginActivity.data);
                income.setIn_Name(income_NameText.getText().toString());
                income.setIn_Date(income_DateText.getText().toString());
                income.setIn_Amount(income_AmountText.getText().toString());
                income.setIn_Remark(income_RemarkText.getText().toString());
                long result = incomeDAO.addIncome(income);
                Log.d(TAG,"add Income result - >" + result);
                assertNotEquals(-1,result);
                Toast.makeText(this,"添加成功，返回上一级",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this,IncomeMainActivity.class));
                break;
            case R.id.pay_exit1:
                Toast.makeText(this,"返回上一级",Toast.LENGTH_SHORT).show();
                startActivity(new Intent(this,IncomeMainActivity.class));
                break;
        }
    }
}
